package main

import (
	"DuckShop/internal/user/database"
	"DuckShop/internal/user/database/dal"
	"DuckShop/internal/user/global"
	"DuckShop/internal/user/handler"
	"DuckShop/internal/user/proto/users"
	"DuckShop/pkg/common"
	"DuckShop/pkg/interceptor"
	"DuckShop/pkg/logger"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"log"
	"net"
	"time"
)

func init() {
	err := common.InitSnowflake(time.Now().Format("2006-01-02"), 1)
	if err != nil {
		log.Fatal(err.Error())
	}
	global.DLog, err = logger.InitLogger("./internal/user/logs/web.log", "internal/user/logs/err.log")
	if err != nil {
		log.Fatal(err.Error())
	}
	global.DuckUserDB, err = database.InitSqlxDB()
	if err != nil {
		log.Fatal(err.Error())
	}
	database.DuckUserGen(global.DuckUserDB)
	dal.SetDefault(global.DuckUserDB)
}
func main() {
	is := grpc.ChainUnaryInterceptor(interceptor.GrpcPanicInterceptor)
	server := grpc.NewServer(is)
	users.RegisterDuckUserServiceServer(server, new(handler.DuckUserServiceServer))
	listen, err := net.Listen("tcp", ":8080")
	if err != nil {
		zap.L().Error(err.Error())
	}
	err = server.Serve(listen)
	if err != nil {
		zap.L().Error(err.Error())
	}
}
